package com.ly.erms.core.log.factory;

import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ly.erms.common.constant.state.LogSucceed;
import com.ly.erms.common.constant.state.LogType;
import com.ly.erms.common.persistence.dao.LoginLogMapper;
import com.ly.erms.common.persistence.dao.OperationLogMapper;
import com.ly.erms.common.persistence.model.LoginLog;
import com.ly.erms.common.persistence.model.OperationLog;
import com.ly.erms.core.db.Db;
import com.ly.erms.core.log.LogManager;
import com.ly.erms.core.util.ToolUtil;

/**
 * 日志操作任务创建工厂
 * @author liuyang
 * @date 2017年12月29日
 */
public class LogTaskFactory {
	private static Logger logger = LoggerFactory.getLogger(LogManager.class);
	private static LoginLogMapper loginLogMapper = Db.getMapper(LoginLogMapper.class);
	private static OperationLogMapper operationLogMapper = Db.getMapper(OperationLogMapper.class);
	
	public static TimerTask loginLog(final Integer userId,final String ip){
		return new TimerTask() {
			
			@Override
			public void run() {
				try {
					LoginLog loginLog = LogFactory.createLoginLog(LogType.LOGIN, userId, null, ip);
					loginLogMapper.insert(loginLog);
				} catch (Exception e) {
					logger.error("创建登陆日志异常",e);
				}
			}
		};
	}
	
	public static TimerTask loginLog(final String username,final String msg,final String ip){
		return new TimerTask(){

			@Override
			public void run() {
				try {
					LoginLog loginLog = LogFactory.createLoginLog(LogType.LOGIN_FAIL, null, "账号："+username +","+msg, ip);
					loginLogMapper.insert(loginLog);
				} catch (Exception e) {
					logger.error("创建登陆失败异常",e);
				}
			}
			
		};
		
	}
	
	public static TimerTask exitLog(final Integer userId,final String ip){
		return new TimerTask() {
			
			@Override
			public void run() {
				try {
					LoginLog loginLog = LogFactory.createLoginLog(LogType.EXIT, userId, null, ip);
					loginLogMapper.insert(loginLog);
				} catch (Exception e) {
					logger.error("创建退出日志异常",e);
				}
			}
		};
	}
	
	  public static TimerTask bussinessLog(final Integer userId, final String bussinessName, final String clazzName, final String methodName, final String msg) {
	        return new TimerTask() {
	            @Override
	            public void run() {
	                OperationLog operationLog = LogFactory.createOperationLog(
	                        LogType.BUSSINESS, userId, bussinessName, clazzName, methodName, msg, LogSucceed.SUCCESS);
	                try {
	                    operationLogMapper.insert(operationLog);
	                } catch (Exception e) {
	                    logger.error("创建业务日志异常!", e);
	                }
	            }
	        };
	    }

	    public static TimerTask exceptionLog(final Integer userId, final Exception exception) {
	        return new TimerTask() {
	            @Override
	            public void run() {
	                String msg = ToolUtil.getExceptionMsg(exception);
	                OperationLog operationLog = LogFactory.createOperationLog(
	                        LogType.EXCEPTION, userId, "", null, null, msg, LogSucceed.FAIL);
	                try {
	                    operationLogMapper.insert(operationLog);
	                } catch (Exception e) {
	                    logger.error("创建异常日志异常!", e);
	                }
	            }
	        };
	    }
}
